package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.utils.ProductUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.business.domain.JtProductPublishSku;
import com.ruoyi.business.mapper.JtProductPublishMapper;
import com.ruoyi.business.domain.JtProductPublish;
import com.ruoyi.business.service.IJtProductPublishService;
import com.ruoyi.common.core.text.Convert;

/**
 * 发布产品信息Service业务层处理
 *
 * @author Alan Zhang
 * @date 2025-04-22
 */
@Service
public class JtProductPublishServiceImpl implements IJtProductPublishService
{
    @Autowired
    private JtProductPublishMapper jtProductPublishMapper;

    /**
     * 查询发布产品信息
     *
     * @param productId 发布产品信息主键
     * @return 发布产品信息
     */
    @Override
    public JtProductPublish selectJtProductPublishByProductId(Long productId)
    {
        return jtProductPublishMapper.selectJtProductPublishByProductId(productId);
    }

    /**
     * 查询发布产品信息列表
     *
     * @param jtProductPublish 发布产品信息
     * @return 发布产品信息
     */
    @Override
    public List<JtProductPublish> selectJtProductPublishList(JtProductPublish jtProductPublish)
    {
        return jtProductPublishMapper.selectJtProductPublishList(jtProductPublish);
    }

    /**
     * 新增发布产品信息
     *
     * @param jtProductPublish 发布产品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJtProductPublish(JtProductPublish jtProductPublish)
    {
        jtProductPublish.setCreateBy(ShiroUtils.getSysUser().getUserName());
        jtProductPublish.setCreateTime(DateUtils.getNowDate());
        int rows = jtProductPublishMapper.insertJtProductPublish(jtProductPublish);
        insertJtProductPublishSku(jtProductPublish);
        return rows;
    }

    /**
     * 修改发布产品信息
     *
     * @param jtProductPublish 发布产品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJtProductPublish(JtProductPublish jtProductPublish, String removeSkuIds)
    {
        jtProductPublish.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        jtProductPublish.setUpdateTime(DateUtils.getNowDate());
        if(StringUtils.isNotEmpty(removeSkuIds)) {
            jtProductPublishMapper.deleteJtProductPublishSkuBySkuIds(Convert.toStrArray(removeSkuIds));
        }
        updateSaveJtProductPublishSku(jtProductPublish);
        return jtProductPublishMapper.updateJtProductPublish(jtProductPublish);
    }

    public void updateSaveJtProductPublishSku(JtProductPublish jtProductPublish){
        List<JtProductPublishSku> jtProductPublishSkuList = jtProductPublish.getJtProductPublishSkuList();
        Long productId = jtProductPublish.getProductId();
        if (StringUtils.isNotNull(jtProductPublishSkuList))
        {
            List<JtProductPublishSku> list = new ArrayList<JtProductPublishSku>();
            for (JtProductPublishSku jtProductPublishSku : jtProductPublishSkuList)
            {
                if(jtProductPublishSku.getProductId() == null) {
                    if(StringUtils.isNotEmpty(jtProductPublishSku.getSkuCode())){
                        boolean res = ProductUtil.checkProductNo(jtProductPublishSku.getSkuCode());
                        if(res){
                            throw new BaseException("SKU编码不能包含特殊字符：逗号、空格");
                        }
                    }
                    jtProductPublishSku.setProductId(productId);
                    jtProductPublishSku.setCreateTime(DateUtils.getNowDate());
                    jtProductPublishSku.setCreateBy(ShiroUtils.getSysUser().getUserName());
                    list.add(jtProductPublishSku);
                } else {
                    jtProductPublishMapper.updateJtProductPublishSku(jtProductPublishSku);
                }
            }
            if (list.size() > 0)
            {
                jtProductPublishMapper.batchJtProductPublishSku(list);
            }
        }
    }

    /**
     * 批量删除发布产品信息
     *
     * @param productIds 需要删除的发布产品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJtProductPublishByProductIds(String productIds)
    {
        jtProductPublishMapper.deleteJtProductPublishSkuByProductIds(Convert.toStrArray(productIds));
        return jtProductPublishMapper.deleteJtProductPublishByProductIds(Convert.toStrArray(productIds));
    }

    /**
     * 删除发布产品信息信息
     *
     * @param productId 发布产品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJtProductPublishByProductId(Long productId)
    {
        jtProductPublishMapper.deleteJtProductPublishSkuByProductId(productId);
        return jtProductPublishMapper.deleteJtProductPublishByProductId(productId);
    }

    /**
     * 新增发布产品SKU信息信息
     *
     * @param jtProductPublish 发布产品信息对象
     */
    public void insertJtProductPublishSku(JtProductPublish jtProductPublish)
    {
        List<JtProductPublishSku> jtProductPublishSkuList = jtProductPublish.getJtProductPublishSkuList();
        Long productId = jtProductPublish.getProductId();
        if (StringUtils.isNotNull(jtProductPublishSkuList))
        {
            List<JtProductPublishSku> list = new ArrayList<JtProductPublishSku>();
            for (JtProductPublishSku jtProductPublishSku : jtProductPublishSkuList)
            {
                jtProductPublishSku.setProductId(productId);
                jtProductPublishSku.setCreateTime(DateUtils.getNowDate());
                jtProductPublishSku.setCreateBy(ShiroUtils.getSysUser().getUserName());
                list.add(jtProductPublishSku);
            }
            if (list.size() > 0)
            {
                jtProductPublishMapper.batchJtProductPublishSku(list);
            }
        }
    }
}
